package xyz.gpgl.erp.sys.controller;

import common.util.base.CommonUtil;
import common.util.mybatis.plus.logic.LogicBaseEmeus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import xyz.gpgl.erp.sys.service.impl.MyUserDetailServiceImpl;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;


@Slf4j
@Controller
public class MainController {

	//类名,字段名,键,说明
	private static Map<String,Map<String, Map<Object,Object>>> dictMap = new HashMap<>();

	@GetMapping("/login")
	public String login(){
		return "login";
	}

	@GetMapping({"/index","/"})
	public String index(Authentication authentication,Model model){
		MyUserDetailServiceImpl.MyUserDetails user = (MyUserDetailServiceImpl.MyUserDetails) authentication.getPrincipal();
		model.addAttribute("name", user.getName());
		String admin = "user";
		for(GrantedAuthority auth:user.getAuthorities()) {
			if(auth.getAuthority().equals("admin")) {
				admin = "admin";
				break;
			}
		}
		model.addAttribute("admin", admin);
		return "index";
	}

	@ResponseBody
	@SuppressWarnings("all")
	@RequestMapping("/dictionary.js")
	public Object dictionary(HttpServletRequest request){
		if(CommonUtil.isEmpty(dictMap)){
			try {
				ServletContext servletContext = request.getSession().getServletContext();
				WebApplicationContext applicationContex = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
				String[] beans = applicationContex.getBeanNamesForType(LogicBaseEmeus.class);
				for (String beanName : beans) {
					Class<?> clazz = applicationContex.getType(beanName);
					Class innerClazz[] = clazz.getDeclaredClasses();
					Map<String,Map<String,Object>> map = new HashMap<>();
					Map<String, Map<Object, Object>> one = dictMap.get(clazz.getSimpleName());
					for (Class cls : innerClazz) {
						if(cls.isEnum()){
							one = CommonUtil.isEmpty(one)? new HashMap<String, Map<Object, Object>>():one;
							Object[] objects  = cls.getEnumConstants();
							Field[] fields = cls.getDeclaredFields();//获取所有字段,包括私有的
							Map<Object, Object> two = one.get(cls.getSimpleName());
							two = CommonUtil.isEmpty(two)? new HashMap<>():two;
							for(Field field:fields){
								String fieldName = field.getName();
								for(Object Obj:objects){
									two.put(cls.getMethod("getValue").invoke(Obj).toString(), cls.getMethod("toString").invoke(Obj));
								}
								one.put(cls.getSimpleName(), two);
							}
							dictMap.put(clazz.getSimpleName(),one);
						}
					}
				}
				log.info("Emeu初始化成功:{}",CommonUtil.convertObjectToJson(dictMap));
			} catch (Exception e) {
				log.error(e.getMessage(),e);
			}
		}
		return dictMap;
	}
}
